package org.shangrila.app.sys.shiro;

import java.util.List;

import org.apache.shiro.SecurityUtils;
import org.shangrila.app.sys.eo.SysAuthority;
import org.shangrila.app.sys.eo.SysRole;
import org.shangrila.app.sys.eo.SysUser;

public class SecUtil {
	
	public static Object getCachedValue(String keys) {	
		return SecurityUtils.getSubject().getSession().getAttribute(keys);	
	}

	public static String getCachedLoginFlag() {
		return (String)SecurityUtils.getSubject().getSession().getAttribute("loginFlag");	
	}

	public static SysUser getCachedShiroUser() {
		return (SysUser)SecurityUtils.getSubject().getSession().getAttribute("shiroUser");	
	}

	@SuppressWarnings("unchecked")
	public static List<SysRole> getCachedUserRoles() {
		return (List<SysRole>)SecurityUtils.getSubject().getSession().getAttribute("userRoles");	
	}

	@SuppressWarnings("unchecked")
	public static List<SysAuthority> getCachedUserSysAuthorities() {
		return (List<SysAuthority>)SecurityUtils.getSubject().getSession().getAttribute("userSysAuthorities");
	}

	@SuppressWarnings("unchecked")
	public static List<String> getCachedShiroRoles() {
		return (List<String>)SecurityUtils.getSubject().getSession().getAttribute("shiroRoles");
	}

	@SuppressWarnings("unchecked")
	public static List<String> getCachedShiroPermissions() {
		return (List<String>)SecurityUtils.getSubject().getSession().getAttribute("shiroPermissions");
	}

	public static String[] getCachedOperatorRoles() {
		SysUser operatorUser = getCachedShiroUser();		
		List<String> shiroRoles = getCachedShiroRoles();
		
		shiroRoles.add(operatorUser.getUserno());
		String[] operatorRoles = new String[shiroRoles.size()];	
		shiroRoles.toArray(operatorRoles);
		
		return operatorRoles;	
	}

}
/*
//save info to session
SecurityUtils.getSubject().getSession().setAttribute("shiroUser", user);
SecurityUtils.getSubject().getSession().setAttribute("userRoles", userRoles);
SecurityUtils.getSubject().getSession().setAttribute("userSysAuthorities",userSysAuthorities);
SecurityUtils.getSubject().getSession().setAttribute("shiroRoles", shiroRoles);
SecurityUtils.getSubject().getSession().setAttribute("shiroPermissions", shiroPermissions);

SysUser operatorUser;
List<SysRole> userRoles;
List<SysAuthority> userSysAuthorities;
List<String> shiroRoles;
List<String> shiroPermissions;
String[] operatorRoles;

operatorUser = (SysUser)SecurityUtils.getSubject().getSession().getAttribute("shiroUser");			
userRoles = (List<SysRole>)SecurityUtils.getSubject().getSession().getAttribute("userRoles");
userSysAuthorities = (List<SysAuthority>)SecurityUtils.getSubject().getSession().getAttribute("userSysAuthorities");
shiroRoles = (List<String>)SecurityUtils.getSubject().getSession().getAttribute("shiroRoles");
shiroPermissions = (List<String>)SecurityUtils.getSubject().getSession().getAttribute("shiroPermissions");

shiroRoles.add(userno);
operatorRoles = new String[shiroRoles.size()];	
shiroRoles.toArray(operatorRoles);
 */
